package org.fhkoeln.sgdm.sudoku.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import org.fhkoeln.sgdm.sudoku.logic.FindBabySteps;
import org.fhkoeln.sgdm.sudoku.logic.FindPairMarkups;
import org.fhkoeln.sgdm.sudoku.logic.FindSingles;
import org.fhkoeln.sgdm.sudoku.logic.SimpleBacktracker;
import org.fhkoeln.sgdm.sudoku.logic.SudokuField;

public class ButtonPanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	public static final String CMD_BACKTRACK = "CmdBacktrack";
	public static final String CMD_STEP = "CmdStep";
	public static final String CMD_LOADSUDOKU = "CmdLoadSudoku";
	
	public ButtonPanel(KeyListener keys)
	{
		JButton curButton = null;
		
		this.setLayout(new FlowLayout());
		
		curButton = new JButton("Next Step");
		curButton.addActionListener(this);
		curButton.setActionCommand(CMD_STEP);
		curButton.addKeyListener(keys);
		
		this.add(curButton);
		
		curButton = new JButton("Solve Sudoku");
		curButton.addActionListener(this);
		curButton.setActionCommand(CMD_BACKTRACK);
		curButton.addKeyListener(keys);
		
		this.add(curButton);
		
		curButton = new JButton("Load Sudoku");
		curButton.addActionListener(this);
		curButton.setActionCommand(CMD_LOADSUDOKU);
		curButton.addKeyListener(keys);
		
		this.add(curButton);
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getActionCommand().equals(CMD_STEP))
		{
			TippFrame.getInstance().addText("Try to find baby steps...");
			if(!new FindBabySteps().findBabySteps(SudokuPanel.getInstance().getSudokuField()))
			{
				TippFrame.getInstance().addText("Could not find baby steps...");
				TippFrame.getInstance().addText("Try to find singles...");
				if(!new FindSingles().findSingles(SudokuPanel.getInstance().getSudokuField()))
				{
					TippFrame.getInstance().addText("Could not find singles...");
					TippFrame.getInstance().addText("Try to find pairs...");
					if(!new FindPairMarkups().findPairings(SudokuPanel.getInstance().getSudokuField()))
					{
						TippFrame.getInstance().addText("Could also not find any pairs... You are on you own!");
					}
				}
			}
		}
		
		if(arg0.getActionCommand().equals(CMD_BACKTRACK))
		{
			SimpleBacktracker sbt = new SimpleBacktracker();
			SudokuField backup = new SudokuField(SudokuPanel.getInstance().getSudokuField());
			SudokuField solver = sbt.backTrack(SudokuPanel.getInstance().getSudokuField());
			if(solver == null)
				solver = backup;
			SudokuPanel.getInstance().setSudokuField(solver);
			MainFrame.instance.doRepaint();
		}
		
		if(arg0.getActionCommand().equals(CMD_LOADSUDOKU))
		{
			try
			{
				JFileChooser fc = new JFileChooser("./sudokus");
				int returnVal = fc.showOpenDialog(this);
				if(returnVal == JFileChooser.APPROVE_OPTION)
				{
					SudokuField newField = new SudokuField();
					newField.loadSudoku(fc.getSelectedFile());
					SudokuPanel.getInstance().setSudokuField(newField);
					MainFrame.instance.doRepaint();
				}
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}
